package com.groupten.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.StudentPaperDao;
import com.groupten.Entity.StudentPaperEntity;
import com.groupten.Entity.QuestionEntity;
import com.groupten.Service.StudentPaperService;
import com.groupten.Vo.IndividualVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("studentPaperService")
public class StudentPaperServiceImpl extends ServiceImpl<StudentPaperDao, StudentPaperEntity> implements StudentPaperService {

    @Autowired
    StudentPaperDao studentPaperDao;

    @Override
    public Integer insertStudentPaper(StudentPaperEntity studentPaperEntity) {
        return baseMapper.insert(studentPaperEntity);
    }

    @Override
    public Integer deleteStudentPaperById(Long StudentPaperId) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(StudentPaperId),StudentPaperEntity::getStupaperId,StudentPaperId);
        return baseMapper.delete(queryWrapper);
    }

    @Override
    public StudentPaperEntity getStudentPaperByStudentNumberAndPaperSession(String studentNumber, Integer paperSession) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(studentNumber),StudentPaperEntity::getStudentNumber,studentNumber);
        queryWrapper.eq(ObjectUtils.isNotEmpty(paperSession),StudentPaperEntity::getPaperSession,paperSession);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<StudentPaperEntity> selectAll(Integer page, Integer limit) {
        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<StudentPaperEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, Wrappers.lambdaQuery());
    }

    @Override
    public IPage<StudentPaperEntity> selectStudentPaperByNumber(String StudentNumber,Integer page, Integer limit) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(StudentNumber),StudentPaperEntity::getStudentNumber,StudentNumber);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<StudentPaperEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<StudentPaperEntity> selectStudentPaperBySession(Integer Session, Integer page, Integer limit) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Session),StudentPaperEntity::getPaperSession,Session);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        //在这里做一个默认排序

        IPage<StudentPaperEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<StudentPaperEntity> selectStudentPaperByClassNumber(String ClassNumber, Integer page, Integer limit) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(ClassNumber),StudentPaperEntity::getClassNumber,ClassNumber);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        //在这里做一个默认排序

        IPage<StudentPaperEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public List<StudentPaperEntity> getStudentPaperByPaperSession(Integer session) {
        LambdaQueryWrapper<StudentPaperEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(session),StudentPaperEntity::getPaperSession,session);

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<StudentPaperEntity> getUnAddPaper() {
        return studentPaperDao.getStudentPaperByIsAttendCount();
    }

    @Override
    public void updateIsAddToPaper(String studentNumber) {
        studentPaperDao.updateIsAttendCount(studentNumber);
    }

    @Override
    public int rankStudentPaper(String studentNumber, Integer session , Double score) {
        List<StudentPaperEntity> studentPapers = getStudentPaperByPaperSession(session);
        int rank = 1;
        for (StudentPaperEntity studentPaper : studentPapers) {
            if(studentPaper.getScore() > score && !studentPaper.getStudentNumber().equals(studentNumber)) {
                rank++;
            }
        }
        return rank;
    }

    /**
     * 添加答题记录
     * @param paperSession 试卷场次
     * @param answerResult 答题结果，(分数:错题列表)映射，此处有用的部分为分数，该映射仅有一个元素
     * @param studentClass 学生班级
     * @param studentID 学生学号
     */
    public void studentPaperIns(Integer paperSession, Map<Double, List<QuestionEntity>> answerResult, String studentClass, String studentID) {
        StudentPaperEntity studentPaper = new StudentPaperEntity(); //学生考试结果封装类
        Double paperScore = null;
        for (Double score : answerResult.keySet()) paperScore = score;//从映射的键值集合中取score
        if (paperScore == null) paperScore = 0.0;  //防止录入null
        //以下四行设置studentPaper的各个属性
        studentPaper.setPaperSession(paperSession);
        studentPaper.setScore(paperScore);
        studentPaper.setClassNumber(studentClass);
        studentPaper.setStudentNumber(studentID);
        //studentPaper.setIsAttendCount(false);
        //调用Service录入数据
        insertStudentPaper(studentPaper);
    }


    @Override
    public IndividualVo FindMaxAndMin(IndividualVo individualVo,Integer session) {
        return selectPaperSessionAndSubject(individualVo,session);
    }

    public IndividualVo selectPaperSessionAndSubject(IndividualVo individualVo,Integer session) {
        Integer subject = studentPaperDao.getPaperSubjectBySession(session);

        List<Integer> sameSubjectPaper = studentPaperDao.getPaperSessionByPaperSubject(subject);

        List<Integer> allStudentPapers = studentPaperDao.getStudentPapers(individualVo.getStudentNumber());



        for (Integer paperSession : sameSubjectPaper) {
            for (Integer studentPaperSession : allStudentPapers) {
                if(studentPaperSession.equals(paperSession)) {
                    individualVo.setHistoryLowest(individualVo.getHistoryLowest() < studentPaperDao.getStudentPaperScore(paperSession,individualVo.getStudentNumber())
                            ? individualVo.getHistoryLowest() : studentPaperDao.getStudentPaperScore(paperSession,individualVo.getStudentNumber()));
                    individualVo.setHistoryHighest(individualVo.getHistoryHighest() > studentPaperDao.getStudentPaperScore(paperSession,individualVo.getStudentNumber())
                            ? individualVo.getHistoryHighest() : studentPaperDao.getStudentPaperScore(paperSession,individualVo.getStudentNumber()));
                }
            }
        }

        return individualVo;

    }

}
